package com.vueblog.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vueblog.po.Anime;
import com.vueblog.po.Blog;
import com.vueblog.service.AnimeService;
import com.vueblog.vo.SimpleBlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class AnimeController {
    private final int pageSize=5;

    @Autowired
    private AnimeService animeService;

    @GetMapping("/getAnimeByKeyword")
    public Object getAnime(String keyword, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        JSONObject jsonObject = new JSONObject();
        String orderBy = "watch_time desc";
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<Anime> animes = animeService.findAnimeByName(keyword);
        PageInfo<Anime> pageInfo = new PageInfo<Anime>(animes);
        jsonObject.put("pageInfo",pageInfo);
        return jsonObject;
    }

    @GetMapping("/getAnimeById")
    public Object getAnimeById(@RequestParam int id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("anime",animeService.getAnimeById(id));
        return jsonObject;
    }

    @GetMapping("/deleteAnime")
    public Object deleteAnime(@RequestParam int id){
        JSONObject jsonObject = new JSONObject();
        int i = animeService.deleteAnime(id);
        if (i>0){
            jsonObject.put("status",200);
            jsonObject.put("msg","删除成功");
        }else {
            jsonObject.put("msg","删除失败，发生未知错误");
        }
        return jsonObject;
    }

    @PostMapping("/updateAnime")
    public Object updateAnime(@RequestBody Anime anime){
        JSONObject jsonObject = new JSONObject();
        int i = animeService.updateAnime(anime);
        if (i>0){
            jsonObject.put("status",200);
            jsonObject.put("msg","更新成功");
        }else {
            jsonObject.put("status",500);
            jsonObject.put("msg","更新失败，发生未知错误");
        };
        return jsonObject;
    }

    @PostMapping("/saveAnime")
    public Object saveAnime(@RequestBody Anime anime){
        JSONObject jsonObject = new JSONObject();
        int i = animeService.saveAnime(anime);
        if (i>0){
            jsonObject.put("status",200);
            jsonObject.put("msg","保存成功");
        }else {
            jsonObject.put("status",500);
            jsonObject.put("msg","保存失败，发生未知错误");
        }
        return jsonObject;
    }
}
